<template>
  <div id="login">
    <div class="demo_name">
      <img src="../../../assets/image/erp.png" alt="" />
    </div>
    <div></div>
    <div class="logon-box">
      <!-- 表单区域 -->
      <div class="form-box">
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginFormRules"
          label-width="0px"
        >
          <!-- 用户名 -->
          <el-form-item prop="userName">
            <el-input
              v-model="loginForm.userName"
              prefix-icon="iconfont icon-user"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="passWord">
            <el-input
              type="password"
              v-model="loginForm.passWord"
              prefix-icon="iconfont icon-mima1 "
            ></el-input>
          </el-form-item>
          <div class="verify_box">
            <div class="input_verify">
              <el-form-item prop="code">
                <el-input
                  type="text"
                  placeholder="图形验证码"
                  v-model="loginForm.verifyCode"
                ></el-input>
              </el-form-item>
            </div>
            <img
              class="verify"
              :src="imgUrl"
              :alt="imgUrl"
              @click="changeVerify"
            />
          </div>
          <!-- 按钮区域 -->
          <el-form-item style="width: 100%">
            <el-button
              type="primary"
              style="width: 100%"
              @click="submitButton()"
              >立即登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>>

<script>
import axios from "axios";
import { loginValidation } from "network/login";
import { lrsLogin } from "network/http";
// import { getVerifyCode } from "network/goods/verifyCode";

export default {
  name: "Login",
  data() {
    return {
      // 这是表单数据登录的数据绑定对象
      imgUrl: "",
      loginForm: {
        userName: "yangming",
        passWord: "1234qwer",
        verifyCode: "",
      },
      // 这是表单验证
      loginFormRules: {
        userName: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        passWord: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        verifyCode: [
          { required: true, message: "请输入验证码", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getVerifyCode();
  },
  methods: {
    // 表单重置按钮
    resetButton() {
      this.$refs.loginFormRef.resetFields();
    },
    // 获取验证码
    getVerifyCode() {
      axios
        .get("/API/images/captcha?data=1603099616023", { responseType: "blob" })
        .then((res) => {
          console.log(res);
          this.imgUrl = window.URL.createObjectURL(res.data);
        });
    },
    // 点击更换验证码
    changeVerify() {
      this.getVerifyCode();
    },
    //表单提交按钮
    submitButton() {
        this.$refs.loginFormRef.validate((valid) => {
          if (!valid) return;
          lrsLogin(this.loginForm.userName, this.loginForm.passWord, this.loginForm.verifyCode).then(
            (res) => {
              console.log(res);
              if (res.data.code !== 200) {
                this.$message.error("登录失败");
              } else {
                this.$message.success("登录成功");
                // 因为token 只有在页面打开期间生效 所以要保存在sessionStorage中
                console.log(res.data.data)
                window.sessionStorage.setItem("token", res.data.data);
                this.$router.push("./elementHome");
              }
            },
            (err) => {
              console.log(err);
            }
          );
        });
    },
  },
};
</script>>

<style lang="less" scoped>
#login {
  width: 100%;
  height: 100%;
  padding-top: 7%;
  background-image: url("../../../assets/image/bg.png");
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}
.demo_name {
  width: 264px;
  height: 44px;
  margin: 0 auto;
  margin-bottom: 40px;
}
.demo_name img {
  width: 100%;
  height: 100%;
}
.verify {
  height: 40px;
}
.verify :hover {
  cursor: pointer;
}
.verify_box {
  display: flex;
}
.logon-box {
  width: 350px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.input_verify {
  width: 70%;
}
.logon-img {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 100%;
  padding: 10px;
  box-shadow: 0 0 2px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  img {
    width: 100%;
    height: 100%;
    border-radius: 100%;
  }
}
.form-box {
  width: 100%;
  padding: 0 20px;
  position: absolute;
  bottom: 0;
  box-sizing: border-box;
}
.btn-group {
  display: flex;
  justify-content: flex-end;
}
</style>
